package com.cooper.mediadb.online;

import android.content.Context;
import android.util.Log;

import com.cooper.scraper.DownloadException;
import com.cooper.scraper.DownloadParams;
import com.cooper.scraper.DownloadPublishRunner;
import com.cooper.scraper.ResourceCollector;
import com.cooper.scraper.ResourceScraper;
import com.cooper.scraper.ResourceScraperRunner;
import com.cooper.scraper.ResultUrl;
import com.cooper.scraper.RunnerThread;
import com.cooper.scraper.ScraperException;

public class OnlineMediaScrapeRunner extends DownloadPublishRunner implements
		ResourceScraperRunner<OnlineMedia> {

	private static OnlineMediaScrapeRunner instance = null;

	public static void initConfigure(DownloadParams params) {
		instance = new OnlineMediaScrapeRunner(params);
	}

	public static OnlineMediaScrapeRunner getInstance() {
		if (instance == null) {
			instance = new OnlineMediaScrapeRunner();
		}
		return instance;
	}

	public static void destroyInstance() {
		if (instance != null) {
			instance.destroy();
		}
	}

	public OnlineMediaScrapeRunner(DownloadParams params) {
		super(params);
	}

	public OnlineMediaScrapeRunner() {
		super();
	}

	public void listResource(ResourceCollector<OnlineMedia> resultMedia)
			throws ScraperException {
		run(resultMedia.generateKey(), new DownloadResultMovies(params,
				resultMedia));
	}

	class DownloadResultMovies extends RunnerThread {

		private ResourceCollector<OnlineMedia> resultMedias;

		public DownloadResultMovies(DownloadParams params,
				ResourceCollector<OnlineMedia> resultMedias) {
			super(params);
			this.resultMedias = resultMedias;
		}

		@Override
		public void run() {
			super.run();
			try {
				ResultUrl resultUrl = resultMedias.scraperListUrl();

				String redirectUrl = httpDownload.getRedirectUrl(resultUrl
						.getUrl());

				boolean redirect = false;
				if (!redirectUrl.equals(resultUrl.getUrl())) {
					resultUrl.setUrl(redirectUrl);
					redirect = true;
				}

				String page = httpDownload.downloadPage(resultUrl.getUrl(),
						resultUrl.getEncodingType(), false);
				if (!Thread.currentThread().isInterrupted()) {
					if (redirect) {
						resultMedias.scraperResult(page, resultUrl.getUrl(),
								redirectUrl);
					} else
						resultMedias.scraperResult(page, resultUrl.getUrl());
				}
				complete(resultMedias.generateKey(), resultMedias);
			} catch (DownloadException e) {
				fail(resultMedias.generateKey(), e.getErrorCode());
			} catch (ScraperException e) {
				fail(resultMedias.generateKey(), e.getErrorCode());
			} catch (Exception e) {
				Log.e("DownloadResultMovies.run", "result:" + resultMedias
						+ "resultUrl:", e);
				fail(resultMedias.generateKey(), DownloadException.ERROR_OTHER);
			}

		}
	}

	public void getMovieDetails(OnlineMediaScraper scraper, OnlineMedia movie) {
		run(movie.generateSubscribedKey(), new DownloadMediaDetails(params,
				scraper, movie));
	}

	class DownloadMediaDetails extends RunnerThread {

		private OnlineMedia movie;

		private ResourceScraper<OnlineMedia> scraper;

		public DownloadMediaDetails(DownloadParams params,
				OnlineMediaScraper scraper, OnlineMedia movie) {
			super(params);
			this.scraper = scraper;
			this.movie = movie;
		}

		@Override
		public void run() {
			super.run();
			try {
				String page = httpDownload.downloadPage(movie.getDetailsUrl(),
						movie.getDetailsPageEncodeType());
				complete(movie.generateSubscribedKey(),
						scraper.extractDetails(page, movie.getId()));
			} catch (DownloadException e) {
				fail(movie.generateSubscribedKey(), e.getErrorCode());
			} catch (ScraperException e) {
				fail(movie.generateSubscribedKey(), e.getErrorCode());
			} catch (Exception e) {
				Log.e("DownloadMediaDetails.run", "scraper:" + scraper
						+ "result:" + movie.getTitle() + "resultUrl:", e);
				fail(movie.generateSubscribedKey(),
						DownloadException.ERROR_OTHER);
			}
		}
	}

	public void analyzeMovieUrl(Context context, MediaSource sources,
			String format) {
		run(sources.getUrl(), new AnalyzeVideoThread(params, sources, format));
	}

}
